package com.xxxx.crm.controller;

import com.xxxx.crm.base.BaseController;
import com.xxxx.crm.base.ResultInfo;
import com.xxxx.crm.query.DepartmentQuery;
import com.xxxx.crm.service.DepartmentService;
import com.xxxx.crm.vo.Department;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("department")
public class DepartmentController extends BaseController {
    @Resource
    private DepartmentService departmentService;

    //部门管理数据查询（分页多条件查询）
//如果flag的值不为空，且值为1，则表示当前查询的是部门管理；否则查询设备管理数据
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object>queryDepartmentByParams(DepartmentQuery departmentQuery,Integer flag, HttpServletRequest request){
        // 判断flag的值
//        if (flag != null && flag == 1) {
//            // 查询设备管理
//            // 设置指派人（当前登录用户的ID）
//            // 从cookie中获取当前登录用户的ID
//            Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
//            departmentQuery.setLinkMan(userId);
//        }
        return departmentService.queryDepartmentByParams(departmentQuery);
    }
    //进入部门管理页面
    @RequestMapping("index")
    public String index(){
        return "department/department";
    }

    //添加部门
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addDepartment(Department department){
        //调用service层的添加方法
        departmentService.addDepartment(department);
        return success("部门添加成功!");

    }

    //更新部门
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateDepartment(Department department){
        //调用service层的添加方法
        departmentService.updateDepartment(department);
        return success("部门更新成功!");

    }

    //进入添加或者修改部门页面
    @RequestMapping("toDepartmentPage")
    public String toDepartmentPage(Integer departmentId, HttpServletRequest request){
        //判断departmentId是否为空
        if(departmentId !=null){
            Department department=departmentService.selectByPrimaryKey(departmentId);
            //将数据设置到请求鱼中
            request.setAttribute("department",department);
        }
        return "department/add_update";
    }

    /*
        删除部门
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteDepartment(Integer[] ids) {
        // 调用Service层的删除方法
        departmentService.deleteBatch(ids);
        return success("部门数据删除成功！");
    }

}
